using System;
using System.Reflection;
using System.Web.UI;

namespace Framework.Library.Core.Utils.Web
{
    public static class ScriptManagerHelper
    {
        private static readonly object _initLock = new object();
        private static bool _initialized;
        private static MethodInfo _registerClientScriptBlockMethod;
        private static MethodInfo _registerStartupScriptMethod;
        private static MethodInfo _registerClientScriptResourceMethod;
        private static bool _ajaxAvailable;

        public static bool AjaxAvailable
        {
            get
            {
                Initialize();

                return _ajaxAvailable;
            }
        }

        private static void Initialize()
        {
            if (!_initialized)
            {
                lock (_initLock)
                {
                    if (!_initialized)
                    {
                        Type scriptManagerType =
                            Type.GetType(
                                "System.Web.UI.ScriptManager, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
                                false);

                        if (scriptManagerType != null)
                        {
                            Type[] parms;

                            parms = new[] {typeof (Control), typeof (Type), typeof (string)};
                            _registerClientScriptResourceMethod =
                                scriptManagerType.GetMethod("RegisterClientScriptResource", parms);

                            parms = new[]
                                        {
                                            typeof (Control), typeof (Type), typeof (string), typeof (string),
                                            typeof (bool)
                                        };
                            _registerClientScriptBlockMethod = scriptManagerType.GetMethod("RegisterClientScriptBlock",
                                                                                           parms);
                            _registerStartupScriptMethod = scriptManagerType.GetMethod("RegisterStartupScript", parms);

                            _ajaxAvailable = true;
                        }

                        _initialized = true;
                    }
                }
            }
        }

        public static void RegisterClientScriptResource(Control control, Type type, string resourceName)
        {
            Initialize();

            if (_registerClientScriptResourceMethod != null)
                _registerClientScriptResourceMethod.Invoke(null, new object[] {control, type, resourceName});
            else
                control.Page.ClientScript.RegisterClientScriptResource(type, resourceName);
        }

        public static void RegisterClientScriptBlock(Control control, Type type, string key, string script,
                                                     bool addScriptTags)
        {
            Initialize();

            if (_registerClientScriptBlockMethod != null)
                _registerClientScriptBlockMethod.Invoke(null, new object[] {control, type, key, script, addScriptTags});
            else
                control.Page.ClientScript.RegisterClientScriptBlock(type, key, script, addScriptTags);
        }

        public static void RegisterStartupScript(Control control, Type type, string key, string script,
                                                 bool addScriptTags)
        {
            Initialize();

            if (_registerStartupScriptMethod != null)
                _registerStartupScriptMethod.Invoke(null, new object[] {control, type, key, script, addScriptTags});
            else
                control.Page.ClientScript.RegisterStartupScript(type, key, script, addScriptTags);
        }
    }
}